{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "earned-mason",
   "metadata": {},
   "source": [
    "# Classification For Businesses In The European Union"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "infectious-watson",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "figured-shadow",
   "metadata": {},
   "source": [
    "The function `clean_eu_nace()` cleans a column containing Classification for businesses in the European Union (NACE) strings, and standardizes them in a given format. The function `validate_eu_nace()` validates either a single NACE strings, a column of NACE strings or a DataFrame of NACE strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "korean-litigation",
   "metadata": {},
   "source": [
    "NACE strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"6201\"\n",
    "* `standard`: NACE strings with proper whitespace in the proper places, like \"62.01\"\n",
    "* `label`: return the category label for the number, like \"Computer programming activities\".\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_eu_nace()` and `validate_eu_nace()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "incomplete-nursery",
   "metadata": {},
   "source": [
    "### An example dataset containing NACE strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "scenic-homeless",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"nace\": [\n",
    "            \"6201\",\n",
    "            \"99999999999\",\n",
    "            \"999 999 999\",\n",
    "            \"004085616\",\n",
    "            \"002 724 334\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\",\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"1111 S Figueroa St, Los Angeles, CA 90015\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "parental-basis",
   "metadata": {},
   "source": [
    "## 1. Default `clean_eu_nace`\n",
    "\n",
    "By default, `clean_eu_nace` will clean nace strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "honest-constitutional",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_eu_nace\n",
    "clean_eu_nace(df, column = \"nace\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "urban-hunger",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "middle-burst",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "greenhouse-beach",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "connected-rider",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_eu_nace(df, column = \"nace\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "transsexual-omaha",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "classified-exposure",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_eu_nace(df, column = \"nace\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "wrapped-texture",
   "metadata": {},
   "source": [
    "### `label`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "foreign-dance",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_eu_nace(df, column = \"nace\", output_format=\"label\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "buried-importance",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned NACE strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "blocked-recorder",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_eu_nace(df, column=\"nace\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "strong-marble",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "narrow-contest",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "objective-cowboy",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_eu_nace(df, \"nace\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "streaming-horse",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "diverse-filling",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_eu_nace(df, \"nace\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "regional-raising",
   "metadata": {},
   "source": [
    "## 4. `validate_eu_nace()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "immediate-christian",
   "metadata": {},
   "source": [
    "`validate_eu_nace()` returns `True` when the input is a valid NACE. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_eu_nace()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_eu_nace()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_eu_nace()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "productive-toddler",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_eu_nace\n",
    "print(validate_eu_nace(\"6201\"))\n",
    "print(validate_eu_nace(\"99999999999\"))\n",
    "print(validate_eu_nace(\"999 999 999\"))\n",
    "print(validate_eu_nace(\"51824753556\"))\n",
    "print(validate_eu_nace(\"004085616\"))\n",
    "print(validate_eu_nace(\"hello\"))\n",
    "print(validate_eu_nace(np.nan))\n",
    "print(validate_eu_nace(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "built-import",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "amazing-regulation",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_eu_nace(df[\"nace\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "native-explosion",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "artificial-blackberry",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_eu_nace(df, column=\"nace\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "confirmed-operations",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "joint-swedish",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_eu_nace(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "original-network",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
